<?php

namespace App\Admin\Controllers;

use App\Models\Category;
use App\Models\Dict;
use App\Models\Task;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TaskController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Task(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('type')->display(function ($v){
                return Dict::where('namespace','task_type')->where('key',$v)->value('value');
            });
            $grid->column('category_id')->display(function ($v){
                return Category::where('id',$v)->value('name');
            });
            $grid->column('singular');
            $grid->column('price');
            $grid->column('valid_time')->display(function ($v){
                return Dict::where('namespace','valid_time')->where('key',$v)->value('value');
            });
            $grid->column('is_recommend')->switch();
            $grid->column('is_good')->switch();
            $grid->column('status')->display(function ($v){
                return Dict::where('namespace','task_status')->where('key',$v)->value('value');
            });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Task(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('category_id');
            $show->field('singular');
            $show->field('price');
            $show->field('valid_time');
            $show->field('is_recommend');
            $show->field('is_good');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Task(), function (Form $form) {
            $form->display('id');
            $form->text('type');
            $form->text('category_id');
            $form->text('singular');
            $form->text('price');
            $form->text('valid_time');
            $form->switch('is_recommend');
            $form->switch('is_good');
            $form->text('status');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
